package hcnet;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import hcnet.model.PassThroughResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Author zhangtao
 * @create 2022/4/20 18:13
 */
@Slf4j
public class Demo {
    private static String GET_CAPABILITY = "GET /ISAPI/Ezviz/SmartControl/HUB/GetCapability";
    private static String GET_HUB_DEV_STATUS = "GET /ISAPI/Ezviz/SmartControl/HUB/GetDevStatus";
    private static String GET_CHILD_DEV_STATUS = "GET /ISAPI/Ezviz/SmartControl/HUB/GetChildDevStatus";
    private static String GET_CHILD_DEV_LIST = "GET /ISAPI/Ezviz/SmartControl/HUB/GetChildDevList";
    private static String GET_DEV_STATUS = "GET /ISAPI/Ezviz/SmartControl/Socket/GetDevStatus";
    private static String SWITCH_CTRL = "PUT /ISAPI/Ezviz/SmartControl/Socket/SwitchCtrl";

    public static void main(String[] args) {
        String ip = "192.168.72.51";
        String userName = "admin";
        String password = "EWPYEV";
        int port = 8000;
        HikDevice device = new HikDevice(HCNetSDK.INSTANCE, ip, port, userName, password);

        HikResult<PassThroughResponse> getCapability = device.passThrough(GET_CAPABILITY, "");
        log.error("getCapability:{}", getCapability.getData().getStringData());

        HikResult<PassThroughResponse> getHubDevStatus = device.passThrough(GET_HUB_DEV_STATUS, "");
        log.error("getHubDevStatus:{}", getHubDevStatus.getData().getStringData());

        HikResult<PassThroughResponse> result = device.passThrough(GET_CHILD_DEV_LIST, "");
        List<ChildDevRsp> childDevListRsp = null;
        if (result.isSuccess()) {
            log.error("设备列表：" + result.getData().getStringData());
            childDevListRsp = new Gson().fromJson(result.getData().getStringData(), new TypeToken<List<ChildDevRsp>>() {
            }.getType());
        }

        if (null != childDevListRsp && childDevListRsp.size() > 0) {
            //查询插座电量
            childDevListRsp.forEach(childDevRsp -> {
                HikResult<PassThroughResponse> resultMsgTmp = device.passThrough(GET_DEV_STATUS + "?child_serial_no=" + childDevRsp.getChild_serial_no(), null);
                log.error("插座：{}", resultMsgTmp.getData().getStringData());
                if (resultMsgTmp.isSuccess()) {
                    DevStausRsp devStausRsp = new Gson().fromJson(resultMsgTmp.getData().getStringData(), DevStausRsp.class);
                    //0,掉电恢复不使能， 1： 掉电恢复使能
                    if (devStausRsp.getPower_off_memory_enable() == 0) {

                    }
                    SwitchCtrl switchCtrl = new SwitchCtrl();
                    switchCtrl.setDomain(1001);
                    switchCtrl.setCmd(18699);
                    Params params = new Params();
                    params.setChannelNo(0);
                    params.setEnable(1);
                    params.setType(14);
                    switchCtrl.setParams(params);
                    HikResult<PassThroughResponse> setMsg =
                            device.passThrough(SWITCH_CTRL + "?child_serial_no=" + childDevRsp.getChild_serial_no(), new Gson().toJson(switchCtrl));
                    log.error("插座控制：{}", setMsg.isSuccess());
                }
            });
        }

        device.destroy();
    }
}
